package com.controller;

import com.pojo.Account;
import com.service.AccountService;
import com.util.ConstUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/27 - 15:01
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    @Resource
    private AccountService accountService;

    /**
     * 添加(注册)用户
     *
     * @param account
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public Map<String, Object> addAccount(Account account) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (accountService.addAccount(account) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "恭喜，注册成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "很遗憾，注册失败，请重试！");
        }
        return maps;
    }

    /**
     * 检查注册的用户名称是否重复
     *
     * @param loginName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkRegisterName")
    public Map<String, Object> checkRegisterName(String loginName) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (accountService.findAccountById(loginName) == null) {
            maps.put(ConstUtils.EXIST, true);
            maps.put(ConstUtils.MESSAGE, "恭喜，用户名可用！");
        } else {
            maps.put(ConstUtils.EXIST, false);
            maps.put(ConstUtils.MESSAGE, "很抱歉，用户名已存在！");
        }
        return maps;
    }
}
